﻿using BaseUi.ViewModel;
using HandyControl.Controls;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi
{
    /// <summary>
    /// 设备手动信息
    /// </summary>
    public partial class DeviceView : UserControlBase
    {
        DeviceViewModel viewModel = new DeviceViewModel();

        public DeviceView()
        {
            InitializeComponent();
            //viewModel.plc = LocalStatic.Plc;
            this.DataContext = viewModel;
        }

        //第一次显示，渲染Ui
        private void visfir(object sender, EventArgs e)
        {
            if (IsInDesignMode)
                return;

            var dotPath = LocalFile.PlcDotPath;
            if (!System.IO.File.Exists(dotPath))
            {
                Growl.Info($"没有找到文档[{dotPath}]，请联系管理员。");
                return;
            }

            //读Excel
            viewModel.DataReads = ExcelDeviceReadModel.GetDatas(dotPath);
            viewModel.DataWrites = ExcelDeviceWriteModel.GetDatas(dotPath);
            viewModel.DataUrns = ExcelDeviceUrnModel.GetDatas(dotPath);
            viewModel.DataServos = ExcelDeviceServoModel.GetDatas(dotPath);

            //加载页面
            ReadUi(viewModel.DataReads);
            WriteUi(viewModel.DataWrites);
            UrnUi(viewModel.DataUrns);
            ServoUi(viewModel.DataServos);

            //展示限制
            viewModel.IsVisRead = viewModel.DataReads.Any();
            viewModel.IsVisWrite = viewModel.DataWrites.Any();
            viewModel.IsVisUrn = viewModel.DataUrns.Any();
            viewModel.IsVisServo = viewModel.DataServos.Any();

            Task.Run(viewModel.WhileRead);
        }

        private void vis(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (IsInDesignMode)
                return;

            viewModel.IsVis = (bool)e.NewValue;
        }

        #region 渲染页面
        void ReadUi(IEnumerable<DeviceReadModel> data)
        {
            stackPanel1.Children.Clear();
            if (data == null || !data.Any())
                return;

            var moren = data.Where(o => string.IsNullOrWhiteSpace(o.ExcelTag.组名));
            var group = data.Where(o => !string.IsNullOrWhiteSpace(o.ExcelTag.组名)).GroupBy(o => o.ExcelTag.组名);

            if (moren.Any())
            {
                WrapPanel wrapPanel = new WrapPanel();
                foreach (var item in moren)
                {
                    item.Value = item.ExcelTag.类型.Trim().ToLower() == "bool" ? false : "-";
                    item.IsExpanded = true;

                    IotStateInfo iotState = new IotStateInfo();
                    iotState.DataContext = item;
                    iotState.SetBinding(IotStateInfo.HeaderProperty, nameof(item.Name));
                    iotState.SetBinding(IotStateInfo.ValueProperty, nameof(item.Value));
                    iotState.SetBinding(IotStateInfo.PostfixProperty, $"{nameof(item.ExcelTag)}.{nameof(item.ExcelTag.单位)}");

                    wrapPanel.Children.Add(iotState);
                }
                stackPanel1.Children.Add(wrapPanel);
            }
            if (group.Any())
            {
                foreach (var item in group)
                {
                    Expander expander = new Expander();
                    expander.Header = item.Key;
                    expander.Tag = item;
                    expander.Expanded += (s, e) =>
                    {
                        var c = (IGrouping<string, DeviceReadModel>)((Expander)s).Tag;
                        foreach (var item in c)
                        {
                            item.IsExpanded = true;
                        }
                    };
                    expander.Collapsed += (s, e) =>
                    {
                        var c = (IGrouping<string, DeviceReadModel>)((Expander)s).Tag;
                        foreach (var item in c)
                        {
                            item.IsExpanded = false;
                        }
                    };

                    WrapPanel wrapPanel = new WrapPanel();
                    foreach (var item2 in item)
                    {
                        item2.Value = item2.ExcelTag.类型.Trim().ToLower() == "bool" ? false : "-";

                        IotStateInfo iotState = new IotStateInfo();
                        iotState.DataContext = item2;
                        iotState.SetBinding(IotStateInfo.HeaderProperty, nameof(item2.Name));
                        iotState.SetBinding(IotStateInfo.ValueProperty, nameof(item2.Value));
                        iotState.SetBinding(IotStateInfo.PostfixProperty, $"{nameof(item2.ExcelTag)}.{nameof(item2.ExcelTag.单位)}");

                        wrapPanel.Children.Add(iotState);
                    }
                    expander.Content = wrapPanel;
                    stackPanel1.Children.Add(expander);
                }
            }
        }

        void WriteUi(IEnumerable<DeviceWriteModel> data)
        {
            if (true)
            {
                stackPanel2.Children.Clear();
                if (data == null || !data.Any())
                    return;

                var moren = data.Where(o => string.IsNullOrWhiteSpace(o.ExcelTag.组名));
                var group = data.Where(o => !string.IsNullOrWhiteSpace(o.ExcelTag.组名)).GroupBy(o => o.ExcelTag.组名);

                if (moren.Any())
                {
                    WrapPanel wrapPanel = new WrapPanel();
                    foreach (var item in moren)
                    {
                        item.Value = item.ExcelTag.类型.Trim().ToLower() == "bool" ? false : "-";
                        item.IsExpanded = true;

                        IotStateInfo iotState = new IotStateInfo();
                        iotState.DataContext = item;
                        iotState.Click += viewModel.WriteClick;
                        iotState.PreviewMouseLeftButtonDown += viewModel.LeftDown;
                        iotState.PreviewMouseLeftButtonUp += viewModel.LeftUp;
                        iotState.SetBinding(IotStateInfo.HeaderProperty, nameof(item.Name));
                        iotState.SetBinding(IotStateInfo.ValueProperty, nameof(item.Value));
                        iotState.SetBinding(IotStateInfo.PostfixProperty, $"{nameof(item.ExcelTag)}.{nameof(item.ExcelTag.单位)}");

                        wrapPanel.Children.Add(iotState);
                    }
                    stackPanel2.Children.Add(wrapPanel);
                }
                if (group.Any())
                {
                    foreach (var item in group)
                    {
                        Expander expander = new Expander();
                        expander.Header = item.Key;
                        expander.Tag = item;
                        expander.Expanded += (s, e) =>
                        {
                            var c = (IGrouping<string, DeviceWriteModel>)((Expander)s).Tag;
                            foreach (var item in c)
                            {
                                item.IsExpanded = true;
                            }
                        };
                        expander.Collapsed += (s, e) =>
                        {
                            var c = (IGrouping<string, DeviceWriteModel>)((Expander)s).Tag;
                            foreach (var item in c)
                            {
                                item.IsExpanded = false;
                            }
                        };

                        WrapPanel wrapPanel = new WrapPanel();
                        foreach (var item2 in item)
                        {
                            item2.Value = item2.ExcelTag.类型.Trim().ToLower() == "bool" ? false : "-";

                            IotStateInfo iotState = new IotStateInfo();
                            iotState.DataContext = item2;
                            iotState.Click += viewModel.WriteClick;
                            iotState.PreviewMouseLeftButtonDown += viewModel.LeftDown;
                            iotState.PreviewMouseLeftButtonUp += viewModel.LeftUp;
                            iotState.SetBinding(IotStateInfo.HeaderProperty, nameof(item2.Name));
                            iotState.SetBinding(IotStateInfo.ValueProperty, nameof(item2.Value));
                            iotState.SetBinding(IotStateInfo.PostfixProperty, $"{nameof(item2.ExcelTag)}.{nameof(item2.ExcelTag.单位)}");

                            wrapPanel.Children.Add(iotState);
                        }
                        expander.Content = wrapPanel;
                        stackPanel2.Children.Add(expander);
                    }
                }
            }
        }

        void UrnUi(IEnumerable<DeviceUrnModel> data)
        {
            if (true)
            {
                stackPanel3.Children.Clear();
                if (data == null || !data.Any())
                    return;

                var moren = data.Where(o => string.IsNullOrWhiteSpace(o.ExcelTag.组名));
                var group = data.Where(o => !string.IsNullOrWhiteSpace(o.ExcelTag.组名)).GroupBy(o => o.ExcelTag.组名);

                if (moren.Any())
                {
                    WrapPanel wrapPanel = new WrapPanel();
                    foreach (var item in moren)
                    {
                        item.IsExpanded = true;

                        IotUrnMode iotUrn = new IotUrnMode();
                        iotUrn.DataContext = item;
                        iotUrn.SetBinding(IotUrnMode.TextProperty, nameof(item.Name));
                        iotUrn.SetBinding(IotUrnMode.IsButBadge1Property, nameof(item.IsGoTo));
                        iotUrn.SetBinding(IotUrnMode.IsButBadge2Property, nameof(item.IsRetTo));
                        iotUrn.Button1.Click += viewModel.Button1_Click;
                        iotUrn.Button1.PreviewMouseLeftButtonDown += viewModel.But1ClickDown;
                        iotUrn.Button1.PreviewMouseLeftButtonUp += viewModel.But1ClickUp;
                        iotUrn.Button2.Click += viewModel.Button2_Click;
                        iotUrn.Button2.PreviewMouseLeftButtonDown += viewModel.But2ClickDown;
                        iotUrn.Button2.PreviewMouseLeftButtonUp += viewModel.But2ClickUp;

                        wrapPanel.Children.Add(iotUrn);
                    }
                    stackPanel3.Children.Add(wrapPanel);
                }
                if (group.Any())
                {
                    foreach (var item in group)
                    {
                        Expander expander = new Expander();
                        expander.Header = item.Key;
                        expander.Tag = item;
                        expander.Expanded += (s, e) =>
                        {
                            var c = (IGrouping<string, DeviceUrnModel>)((Expander)s).Tag;
                            foreach (var item in c)
                            {
                                item.IsExpanded = true;
                            }
                        };
                        expander.Collapsed += (s, e) =>
                        {
                            var c = (IGrouping<string, DeviceUrnModel>)((Expander)s).Tag;
                            foreach (var item in c)
                            {
                                item.IsExpanded = false;
                            }
                        };

                        WrapPanel wrapPanel = new WrapPanel();
                        foreach (var item2 in item)
                        {
                            IotUrnMode iotUrn = new IotUrnMode();
                            iotUrn.DataContext = item2;
                            iotUrn.SetBinding(IotUrnMode.TextProperty, nameof(item2.Name));
                            iotUrn.SetBinding(IotUrnMode.IsButBadge1Property, nameof(item2.IsGoTo));
                            iotUrn.SetBinding(IotUrnMode.IsButBadge2Property, nameof(item2.IsRetTo));
                            iotUrn.Button1.Click += viewModel.Button1_Click;
                            iotUrn.Button1.PreviewMouseLeftButtonDown += viewModel.But1ClickDown;
                            iotUrn.Button1.PreviewMouseLeftButtonUp += viewModel.But1ClickUp;
                            iotUrn.Button2.Click += viewModel.Button2_Click;
                            iotUrn.Button2.PreviewMouseLeftButtonDown += viewModel.But2ClickDown;
                            iotUrn.Button2.PreviewMouseLeftButtonUp += viewModel.But2ClickUp;

                            wrapPanel.Children.Add(iotUrn);
                        }
                        expander.Content = wrapPanel;
                        stackPanel3.Children.Add(expander);
                    }
                }
            }
        }

        void ServoUi(IEnumerable<DeviceServoModel> data)
        {
            if (true)
            {
                stackPanel4.Children.Clear();
                if (data == null || !data.Any())
                    return;

                var moren = data.Where(o => string.IsNullOrWhiteSpace(o.ExcelTag.组名));
                var group = data.Where(o => !string.IsNullOrWhiteSpace(o.ExcelTag.组名)).GroupBy(o => o.ExcelTag.组名);

                if (moren.Any())
                {
                    WrapPanel wrapPanel = new WrapPanel();
                    foreach (var item in moren)
                    {
                        item.IsExpanded = true;

                        IotServoMode iotServo = new IotServoMode();
                        iotServo.DataContext = item;
                        iotServo.SetBinding(IotServoMode.TextProperty, nameof(item.Name));
                        iotServo.SetBinding(IotServoMode.Speed1Property, nameof(item.JogSpeed));
                        iotServo.SetBinding(IotServoMode.Speed2Property, nameof(item.AutoSpeed));
                        iotServo.SetBinding(IotServoMode.LocationProperty, nameof(item.Location));
                        iotServo.SetBinding(IotServoMode.IsVis1Property, new Binding(nameof(item.IsJog)) { Mode = BindingMode.TwoWay });
                        iotServo.SetBinding(IotServoMode.IsFoldProperty, new Binding(nameof(item.IsFold)) { Mode = BindingMode.TwoWay });
                        iotServo.LocationChange += viewModel.LocationChange;
                        iotServo.SpeedChange += viewModel.SpeedChange;

                        wrapPanel.Children.Add(iotServo);
                    }
                    stackPanel4.Children.Add(wrapPanel);
                }
                if (group.Any())
                {
                    foreach (var item in group)
                    {
                        Expander expander = new Expander();
                        expander.Header = item.Key;
                        expander.Tag = item;
                        expander.Expanded += (s, e) =>
                        {
                            var c = (IGrouping<string, DeviceServoModel>)((Expander)s).Tag;
                            foreach (var item in c)
                            {
                                item.IsExpanded = true;
                            }
                        };
                        expander.Collapsed += (s, e) =>
                        {
                            var c = (IGrouping<string, DeviceServoModel>)((Expander)s).Tag;
                            foreach (var item in c)
                            {
                                item.IsExpanded = false;
                            }
                        };

                        WrapPanel wrapPanel = new WrapPanel();
                        foreach (var item2 in item)
                        {
                            IotServoMode iotServo = new IotServoMode();
                            iotServo.DataContext = item2;
                            iotServo.SetBinding(IotServoMode.TextProperty, nameof(item2.Name));
                            iotServo.SetBinding(IotServoMode.Speed1Property, nameof(item2.JogSpeed));
                            iotServo.SetBinding(IotServoMode.Speed2Property, nameof(item2.AutoSpeed));
                            iotServo.SetBinding(IotServoMode.LocationProperty, nameof(item2.Location));
                            iotServo.SetBinding(IotServoMode.IsVis1Property, new Binding(nameof(item2.IsJog)) { Mode = BindingMode.TwoWay });
                            iotServo.SetBinding(IotServoMode.IsFoldProperty, new Binding(nameof(item2.IsFold)) { Mode = BindingMode.TwoWay });
                            iotServo.LocationChange += viewModel.LocationChange;
                            iotServo.SpeedChange += viewModel.SpeedChange;

                            wrapPanel.Children.Add(iotServo);
                        }
                        expander.Content = wrapPanel;
                        stackPanel4.Children.Add(expander);
                    }
                }
            }
        }
        #endregion
    }
}
